﻿Imports System.Web.Configuration
Imports System.Web.Services

Public Class Divisi
    Inherits System.Web.UI.Page
    Dim connectionString As String
    Dim divisiDb As DivisiDb

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connectionString = WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString
        divisiDb = New DivisiDb(connectionString)
        DivisiGridView.DataBind()

        DivisiTableForm.Visible = False
        AddButton.Enabled = True
    End Sub

    Protected Sub DivisiGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles DivisiGridView.RowCommand

        If e.CommandName = "CopyCommand" Then
            ResetState()
            DivisiTableForm.Visible = True
            AddButton.Enabled = False

            KodeDivisi.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            Descripsi.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            Satker.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(3).Text
            If DivisiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf DivisiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If

        ElseIf e.CommandName = "EditCommand" Then
            ResetState()
            DivisiTableForm.Visible = True
            AddButton.Enabled = False

            IDDivisi.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(0).Text
            KodeDivisi.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            Descripsi.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            Satker.Text = DivisiGridView.Rows(e.CommandArgument.ToString).Cells(3).Text
            If DivisiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf DivisiGridView.Rows(e.CommandArgument.ToString).Cells(4).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If

        End If
    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
        If Equals("0", IDDivisi.Text) Then
            If Not divisiDb.isExist(KodeDivisi.Text) Then
                divisiDb.insert(Satker.SelectedValue, KodeDivisi.Text, Descripsi.Text, Status.SelectedValue)
                DivisiGridView.DataBind()
            End If
        Else
            divisiDb.update(IDDivisi.Text, Satker.SelectedValue, KodeDivisi.Text, Descripsi.Text, Status.SelectedValue)
            DivisiGridView.DataBind()
        End If

        ResetState()
    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CancelButton.Click
        ResetState()
    End Sub

    Sub ResetState()
        AddButton.Enabled = True
        IDDivisi.Text = "0"
        KodeDivisi.Text = ""
        Descripsi.Text = ""
        Satker.DataBind()
        Status.SelectedValue = 0
        DivisiGridView.SelectedIndex = -1
        'DivisiGridView.Enabled = True
    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddButton.Click
        ResetState()
        DivisiTableForm.Visible = True
    End Sub

    <WebMethod()>
    Public Shared Function IsExist(ByVal kodeDivisi As String) As String
        Dim divisiDb2 As New DivisiDb(WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString)

        If divisiDb2.isExist(kodeDivisi) Then
            Return "1"
        End If

        Return "0"
    End Function
End Class